#!/bin/bash
#$Id$
#
# pull down market data from eve central
# (c) 2010 cdk@bitmech.com, see LICENSE
# see http://www.eve-central.com/home/develop.html for API reference.
#
# service endpoint
URL=http://eve-central.com/api/quicklook/

# delay (s) between requests -- be nice to eve-central.
DELAY=1

# default update interval (days) upon which to refresh data
UPDATE=7

# default order age (hours) 
AGE=24

# default region (urlencoded)
REGION=""

# default system (urlencoded)
SYSTEM=""

# dir where output will go.
OUTDIR=./output

#
# parse arguments 
# -s[system] -a[age] -u[update]
# -orders within [age] hours
# -age within [update] days
#
args="$@"
OPTIND=1
while getopts ":a:i:u:s:o:" opt $args
do
    case $opt in
      a) AGE="${OPTARG}";;
      i) ITEMS="${OPTARG}";;
      u) UPDATE="${OPTARG}";;
      s) SYSTEM="${OPTARG}";;
      o) OUTDIR="${OPTARG}";;
      *) echo "Usage: $0 -s[system] -a[age hrs] -u[update days] -o[outputdir]";
         echo "          -i[item1,item2,...itemN]";
         echo "Invalid arguments. Exiting."; 
         exit 1;;
    esac
done


#
# staticdb root
STATICDB=../staticdb

#
# CSV hash of item db by typeid
ITEMCSV=${STATICDB}/items-by-id.csv

#
# create output dir if it doesn't exist
if [ ! -d "$OUTDIR" ]; then
    mkdir -p $OUTDIR
    if [ "$?" != "0" ]; then
        echo "Couldn't mkdir output directory $OUTDIR; exiting."
        exit 1 
    fi
fi

#
# current default is to find prices for all *valid* TYPEIDS
TYPEIDS=$(cat ${ITEMCSV} | awk -F, '{print $1}' | xargs)

#
# if IDs are specified, shorten the list 
if [ "${ITEMS}" != "" ]; then
    TYPEIDS=$(echo ${ITEMS} | tr ',' ' ')
    NUMTYPES=$(echo $TYPEIDS | wc -w)
fi

#
# if the list is less than 100 items, shorten the update interval to 1 day
if [ ${NUMTYPES} -lt 100 ]; then
    UPDATE=1
fi

#
# add age of orders in hours
AGEARGS="&sethours=${AGE}"

#
# don't limit to region by default
#if [ "$REGION" != "" ]; then
#    APIARGS="${APIARGS}&regionlimit=${REGION}"
#fi

#
# don't restrict to system by default
if [ "$SYSTEM" != "" ]; then
    SYSTEMARGS="&usesystem=${SYSTEM}"
    echo "Using ${SYSTEM} system."
fi

#
# don't limit by quantity by default
#APIARGS="${APIARGS}&setminQ=5"

#
# if the mentos exist are less than a week old, don't grab them again.
# don't be a dick -- use this or something like it.
for item in $TYPEIDS
do
    OUTFILE=orders-${item}.xml
    OUTPATH=${OUTDIR}/${OUTFILE}
    TYPEARGS="typeid=${item}"
    APIARGS="?${TYPEARGS}${AGEARGS}${SYSTEMARGS}"
    freshness=$(find ${OUTDIR}             \
               -type f -mtime -${UPDATE}   \
               -size +0                    \
               -name "${OUTFILE}"          \
               2>/dev/null)
    #
    # skip items which are not generally found on the market
    if [ ${item} -le 17 ]; then
        echo "Data for ${item} is generally not on the market... skipping."
        continue
    fi
    if [  "$freshness" != "" ]; then
        echo "Data for ${item} had in the last ${UPDATE} days... skipping."
        continue
    else
        echo -n "Getting ${item}, nom nom nom... "
        wget -O ${OUTPATH} "${URL}${APIARGS}" >/dev/null 2>&1
        #echo "wget -O ${OUT} \"${URL}${APIARGS}\" >/dev/null 2>&1"
        echo "done; contents are in ${OUTPATH}."
        sleep ${DELAY}
    fi
done

exit 0


